<?php

namespace App\Models\System\Mo;
use App\Libs\Tools\Excel\ExcelImport;
use App\Libs\Tools\SystemCodeConstant;
use Illuminate\Support\Facades\DB;

class UserExcelImportMO extends ExcelImport
{
    protected $titles=[
        'phone',
        'password',
        'name',
        'sex',
        'email',
        'wx_no',
        'major_department_id',
        'major_station_id',
        ];

    public function convert_results_data(){
        $results=$this->getResults();
        $this->results=[];
        $users=[];
        $departments=[];
        $stations=[];
        foreach ($results as $key=>$result){
            $result['password']=bcrypt($result['password']);
            //格式化性别
            if($result['sex']=='女'){$result['sex']=SystemCodeConstant::SEX_FEMALE;}else{
                $result['sex']=SystemCodeConstant::SEX_MALE;
            }

            if(isset($result['direct_leader_user_id'])){
                $user_name=$result['direct_leader_user_id'];
                if(isset($users[$user_name])){$result['direct_leader_user_id']=$users[$user_name];}else{
                    $user= DB::table('users')->where('name','=',$user_name)->first();
                    if($user) {
                        $users[$user_name]=$user->id;
                    }else{
                        return $result['name']."的上级领导['$user_name']在系统不存在";
                    }
                    $result['direct_leader_user_id']=$users[$user_name];
                }
            }else{
                $result['direct_leader_user_id']=0;
            }

            if(isset($result['major_department_id'])){
                $department_name=$result['major_department_id'];
                if(isset($departments[$department_name])){$result['major_department_id']=$departments[$department_name];}else{
                    $department= DB::table('department')->where('name','=',$department_name)->first();
                    if($department) {
                        $departments[$department_name]=$department->id;
                    }else{
                        return $result['name']."所在部门['$department_name']在系统不存在";
                    }
                    $result['major_department_id']=$departments[$department_name];
                }
            }else{
                $result['major_department_id']=0;
            }

            if(isset($result['major_station_id'])){
                $station_name=$result['major_station_id'];
                if(isset($stations[$station_name])){$result['major_station_id']=$stations[$station_name];}else{
                    $station= DB::table('station')->where('name','=',$station_name)->first();
                    if($station) {
                        $stations[$station_name]=$station->id;
                    }else{
                        return $result['name']."的岗位['$station_name']在系统不存在";
                    }
                    $result['major_station_id']=$stations[$station_name];
                }
            }else{
                $result['major_station_id']=0;
            }
            $query=DB::table('users')->where('phone','=',$result['phone']);
            if(isset($result['email'])){
                $query=$query->orWhere('email','=',$result['email']);
            }
            $cnt=$query->count();
            if($cnt>0){
                return $result['name']."的电话号码或者邮箱已经在系统中存在";
            }
            array_push($this->results,$result);
        }
        return null;
    }
    public function verify(){
       $results=$this->getResults();
       foreach ($results as $key=>$result){
           if(!isset($result['phone']) ||
              !isset($result['password']) ||
              !isset($result['name']) ||
              !isset($result['sex'])){
                return '手机、密码、真实姓名、性别是必填项';
           }
       }
       return null;
    }
}